<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12" v-if="flag">
            <a-form-item label="所属风场" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-depart v-decorator="['orgName', validatorRules.orgName,{initialValue:this.orgCode}]" id="orgname" @change="change"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="flag">
            <a-form-item label="所属项目" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag v-decorator="['productBelong',validatorRules.productBelong]" dict="" setFieldsValue="selectValue" :dictOptions="productBelong" @change="getpb"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="flag">
            <a-form-item label="物料大类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag v-decorator="['codeTypeName',validatorRules.codeTypeName]" dict="" setFieldsValue="selectValue" :dictOptions="equipTypeCode" @change="getetc"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="flag">
            <a-form-item label="物料小类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag v-decorator="['codeSubtypeName',validatorRules.codeSubtypeName]" dict="" setFieldsValue="selectValue" :dictOptions="equipSubtypeCode" />
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="flag">
            <a-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag v-decorator="['productNameValue',validatorRules.productNameValue]" dict="" setFieldsValue="selectValue" :dictOptions="productName"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="flag">
            <a-form-item label="设备厂家" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag v-decorator="['factoryNameValue',validatorRules.factoryNameValue]" dict="" setFieldsValue="selectValue" :dictOptions="factoryName"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="flag">
            <a-form-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag v-decorator="['productTypeName',validatorRules.productTypeName]" dict="" setFieldsValue="selectValue" :dictOptions="productType"/>
            </a-form-item>
          </a-col>
          
          <a-col :span="12">
            <a-form-item label="设备用途" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productUsing',validatorRules.productUsing]" placeholder="请输入设备用途"></a-input>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-item label="库存数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['surplusNum',validatorRules.surplusNum]" placeholder="请输入库存数量"></a-input>
            </a-form-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-item label="告警数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['riskSurplus',validatorRules.riskSurplus]" placeholder="请输入告警数量"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['unitPrice',validatorRules.unitPrice]" placeholder="请输入单价"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="flag">
            <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-search-select-tag v-decorator="['unit',validatorRules.unit]" dict="" setFieldsValue="selectValue" :dictOptions="unit1" />
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="flag">
            <a-form-item label="是否本厂" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['ownOrOthers', validatorRules.ownOrOthers]" :trigger-change="true" dictCode="own_or_others" placeholder="请选择是否本厂" v-on:show="handleChange"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="存放位置" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productArea']" placeholder="请输入存放位置"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="设备编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productEncodings']" placeholder="请输入设备用途"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['remark']" placeholder="请输入备注"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="flag">
            <a-form-item label="文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload v-decorator="['fileName']" :trigger-change="true"></j-upload>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="物料编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productEncoding']" placeholder="请输入物料编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JUpload from '@/components/jeecg/JUpload'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  import store from '@/store'

  export default {
    name: 'DtCProductForm',
    components: {
      JFormContainer,
      JUpload,
      JSelectDepart,
      JDictSelectTag,
      JSearchSelectTag,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        flag:true,
        departIds:'',
        orgCode:'',
        selectValue:"",
        org_code:'',//所在风场选择
        pb:'', //所属项目选择
        etc:'',//物料大类选择
        equipTypeCode:[],
        equipSubtypeCode:[],
        productName:[],
        factoryName:[],
        productType:[],
        productBelong:[],
        unit1:[],
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           orgName: {
            rules: [
              { required: true, message: '请输入所属风场!'},
            ]
          },
           productBelong: {
            rules: [
              { required: true, message: '请输入所属项目!'},
            ]
          },
           codeTypeName: {
            rules: [
              { required: true, message: '请输入物料大类!'},
            ]
          },
           codeSubtypeName: {
            rules: [
              { required: true, message: '请输入物料小类!'},
            ]
          },
           productNameValue: {
            rules: [
              { required: true, message: '请输入物料名称!'},
            ]
          },
           factoryNameValue: {
            rules: [
              { required: true, message: '请输入设备厂家!'},
            ]
          },
           productTypeName: {
            rules: [
              { required: true, message: '请输入规格型号!'},
            ]
          },
           productUsing: {
            rules: [
              { required: true, message: '请输入设备用途!'},
            ]
          },
           surplusNum: {
            rules: [
              { required: true, message: '请输入库存数量!'},
            ]
          },
           riskSurplus: {
            rules: [
              { required: true, message: '请输入告警数量!'},
            ]
          },
          unitPrice: {
            rules: [
              { required: true, message: '请输入单价!'},
            ]
          },
          unit: {
            rules: [
              { required: true, message: '请输入单位!'},
            ]
          },
          ownOrOthers: {
            rules: [
              { required: true, message: '请输入是否本厂!'},
            ]
          },
        },
        url: {
          add: "/dtproduct/dtCProduct/add",
          edit: "/dtproduct/dtCProduct/edit",
          queryById: "/dtproduct/dtCProduct/queryById",
          getequipTypeCode1:"/dtproduct/dtCProduct/queryProductByParam",
       
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        var obj={}
      obj.orgName = store.getters.userInfo.departIds
      console.log(obj)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(obj,'orgName'))  //使添加的时候所属风场是登录人所在的风场
      })
        this.edit({});
      },
      edit (record) {
        if(record.id != null){
          this.flag = false
        }else{
          this.flag = true
        }
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'orgName','codeTypeName','codeSubtypeName','productNameValue','factoryNameValue','productTypeName','productBelong','productUsing','surplusNum','riskSurplus','scrapNum','reposUsed','reposProcess','unitPrice','unit','ownOrOthers','productArea','productEncodings','remark','fileName','productEncoding'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            values.surplusNum = '0'
            values.scrapNum = '0'
            values.reposUsed = '0'
            values.reposProcess = '0'
          
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'orgName','codeTypeName','codeSubtypeName','productNameValue','factoryNameValue','productTypeName','productBelong','productUsing','surplusNum','riskSurplus','scrapNum','reposUsed','reposProcess','unitPrice','unit','ownOrOthers','productArea','productEncodings','remark','fileName','productEncoding'))
      },
     
      handleChange(value) {
        console.log("selected", value); 
        },
        //选择部门之后再
     //选择部门之后再
      change(value){   
        console.log('value',value)
        this.departIds = value

       if(value == ''){
         console.log('没有')
          var obj = {};
          var objlist = [];
          objlist.push(obj);
         this.productBelong = objlist;
         this.equipTypeCode = objlist;
          this.productName = objlist;
          this.productType = objlist;
          this.factoryName = objlist;
       }else{
         this.getproductBelong()
       }
       this.form.setFieldsValue({  //更换风场之后下面的物料大类和小类等都变为空
          productBelong: '',
          codeTypeName:'',
          codeSubtypeName:'',
          productNameValue:'',
          factoryNameValue:'', //设备厂家
          productTypeName:'',
          productUsing:'',//设备用途
          surplusNum:'',//库存数量
          riskSurplus:'',//警告数量
          unitPrice:'',//单价
          unit:'',//单位
          ownOrOthers:'',//是否本场
          productArea:'',//存放位置
          productEncodings:'',//设备用途
        })
      },
      //所属项目选择
      getpb(value){
       console.log('value111111',value)
       if(value == undefined){
         console.log('没选')
          var obj = {};
          var objlist = [];
          objlist.push(obj);
          this.equipTypeCode = objlist;
          this.productName = objlist;
          this.productType = objlist;
          this.factoryName = objlist;
       }else{
         this.pb = value;
        this.getequipTypeCode()
        this.getproductName()
        this.productType1()
        this.getfactoryName1()
        this.getunit()
       }
        
      },
      //物料大类选择
      getetc(value){
        if(value == undefined){
          var obj = {};
          var objlist = [];
          objlist.push(obj);
          this.equipSubtypeCode = objlist

        }else{
           this.etc = value
          this.getequipSubtypeCode()
        }
       
      },

       //物料大类
      getequipTypeCode(){
        this.equipTypeCode = []
       var params = {};
        params.codeType = 'code100000';
        params.codeSortId = '100000';
       //params.content1 = '999999999';
        params.content5 = '';
        params.productBelong = this.pb;
        params. orgName= this.departIds;
        getAction(this.url.getequipTypeCode1, params).then((res) => {
            var objlist = [];
            if (res.success) {
              for(var i=0;i<res.result.length;i++){
                var obj = {};
                obj.value = res.result[i].dtPCode;
                obj.text = res.result[i].dtPValue;
                obj.title = res.result[i].dtPValue;
                objlist.push(obj);
              }
              console.log(objlist)
              this.equipTypeCode = objlist;
            }
            if(res.code===510){
              this.$message.warning(res.message)
            }
         })

      },
      //物料小类
      getequipSubtypeCode(){
        this.equipSubtypeCode.length=0;
        var params = {};
        var objlist = [];
        params.codeType = 'code200000';
        params.codeSortId = '200000';
       //params.content1 = '999999999';
        params.content5 = this.etc;
        params.productBelong = this.pb;
        params. orgName= this.departIds;
        getAction(this.url.getequipTypeCode1, params).then((res) => {
            console.log("res=",res)
            console.log("res.result==<<<<<",res.result)
              if (res.success) {
                  if(res.result.length == 0){
                    this.equipSubtypeCode.length=0;
                    var obj = {};
                      objlist.push(obj);
                      this.equipSubtypeCode = objlist;
                  }else{
                    for(var i=0;i<res.result.length;i++){
                      var obj = {};
                      obj.value = res.result[i].dtPCode;
                      obj.text = res.result[i].dtPValue;
                      obj.title = res.result[i].dtPValue;
                      objlist.push(obj);
                    }
                    console.log(objlist)
                    this.equipSubtypeCode = objlist;
                }
              }
            if(res.code===510){
              this.$message.warning(res.message)
            }
         })
          
      },
      //物料名称
      getproductName(){
         this.equipTypeCode = []
       var params = {};
        params.codeType = 'code500000';
        params.codeSortId = '500000';
       //params.content1 = '999999999';
        params.productBelong = this.pb;
        params.orgName= this.departIds;
        getAction(this.url.getequipTypeCode1, params).then((res) => {
            var objlist = [];
            if (res.success) {
              for(var i=0;i<res.result.length;i++){
                var obj = {};
                obj.value = res.result[i].dtPCode;
                obj.text = res.result[i].dtPValue;
                obj.title = res.result[i].dtPValue;
                objlist.push(obj);
              }
              console.log(objlist)
              this.productName = objlist;
            }
            if(res.code===510){
              this.$message.warning(res.message)
            }
         })

      },
      //设备厂家
      getfactoryName1(){
          this.equipTypeCode = []
       var params = {};
        params.codeType = 'code300000';
        params.codeSortId = '300000';
       //params.content1 = '999999999';
        params.productBelong = this.pb;
        params.orgName= this.departIds;
        getAction(this.url.getequipTypeCode1, params).then((res) => {
            var objlist = [];
            if (res.success) {
              for(var i=0;i<res.result.length;i++){
                var obj = {};
                obj.value = res.result[i].dtPCode;
                obj.text = res.result[i].dtPValue;
                obj.title = res.result[i].dtPValue;
                objlist.push(obj);
              }
              console.log(objlist)
              this.factoryName = objlist;
            }
            if(res.code===510){
              this.$message.warning(res.message)
            }
         })

      },
      //规格型号
      productType1(){
         this.equipTypeCode = []
       var params = {};
        params.codeType = 'code400000';
        params.codeSortId = '400000';
       //params.content1 = '999999999';
        params.productBelong = this.pb;
        params.orgName= this.departIds;
        getAction(this.url.getequipTypeCode1, params).then((res) => {
            var objlist = [];
            if (res.success) {
              for(var i=0;i<res.result.length;i++){
                var obj = {};
                obj.value = res.result[i].dtPCode;
                obj.text = res.result[i].dtPValue;
                obj.title = res.result[i].dtPValue;
                objlist.push(obj);
              }
              console.log(objlist)
              this.productType = objlist;
            }
            if(res.code===510){
              this.$message.warning(res.message)
            }
         })

      },
      //所属项目
      getproductBelong(){
        var params = {};
        params.codeType = 'code600000';
        params.codeSortId = '600000';
       //params.content1 = '999999999';
        params.orgName = this.departIds;
        getAction(this.url.getequipTypeCode1, params).then((res) => {
            var objlist = [];
            if (res.success) {
              for(var i=0;i<res.result.length;i++){
                var obj = {};
                obj.value = res.result[i].dtPCode;
                obj.text = res.result[i].dtPValue;
                obj.title = res.result[i].dtPValue;
                objlist.push(obj);
              }
              console.log(objlist)
              this.productBelong = objlist;
            }
            if(res.code===510){
              this.$message.warning(res.message)
            }
         })
      },
      //单位
      getunit(){
        var params = {};
        params.codeType = 'code800000';
        params.codeSortId = '800000';
       //params.content1 = '999999999';
        params.orgName = this.departIds;
        getAction(this.url.getequipTypeCode1, params).then((res) => {
            var objlist = [];
            if (res.success) {
              for(var i=0;i<res.result.length;i++){
                var obj = {};
                obj.value = res.result[i].dtPCode;
                obj.text = res.result[i].dtPValue;
                obj.title = res.result[i].dtPValue;
                objlist.push(obj);
              }
              console.log(objlist)
              this.unit1 = objlist;
            }
            if(res.code===510){
              this.$message.warning(res.message)
            }
         })

      },
      getpb1(){
          var params = {};
        params.codeType = 'code600000';
        params.codeSortId = '600000';
       //params.content1 = '999999999';
        params.orgName = store.getters.userInfo.departIds;
        getAction(this.url.getequipTypeCode1, params).then((res) => {
            var objlist = [];
            if (res.success) {
              for(var i=0;i<res.result.length;i++){
                var obj = {};
                obj.value = res.result[i].dtPCode;
                obj.text = res.result[i].dtPValue;
                obj.title = res.result[i].dtPValue;
                objlist.push(obj);
              }
              console.log(objlist)
              this.productBelong = objlist;
            }
            if(res.code===510){
              this.$message.warning(res.message)
            }
         })

      }
    },
     mounted(){
       console.log('11111233333444')
      this.getpb1()
      this.orgCode = store.getters.userInfo.orgCode
    }
  }
</script>